package com.example.shirotest.study;


public class Josepful {

    public static void main(String[] args) {
        circleSingleLinkedList circleSingleLinkedList = new circleSingleLinkedList();
        circleSingleLinkedList.addBoy(5);
        circleSingleLinkedList.showBoy();

        //约瑟夫问题的实现
        System.out.println("-------------------------------");
        circleSingleLinkedList.countBoy(1,2,5);


    }

}

//约瑟夫问题（环形单向链表）
class circleSingleLinkedList{
    //创建一个first节点，当前没有编号
    private Boy first = null;

    //添加小孩节点，构成环形链表
    public void addBoy(int nums){
        if(nums < 1){
            System.out.println("nums is error");
            return;
        }
        Boy curBoy = null; //辅助指针，帮助构建环形链表
        //使用for来创建环形链表
        for (int i = 1; i <= nums ; i++) {
            //根据编号，创建小孩节点
            Boy boy = new Boy(i);
            //如果是第一个小孩
            if(i==1){
                first = boy;
                first.setNext(first); //构成环形
                curBoy = first; //让curBoy指向第一个小孩
            } else {
                curBoy.setNext(boy);
                boy.setNext(first);
                curBoy = boy;
            }
        }
    }

    //遍历当前的环形链表
    public void showBoy(){
        if(first == null){
            System.out.println("no boy ~~");
            return;
        }
        //定义辅助指针完成遍历
        Boy curBoy = first;
        while (true){
            System.out.printf("The boy no is %d\n",curBoy.getNo());
            //已经遍历完毕
            if(curBoy.getNext() == first){
                break;
            }
            //curBoy 后移
            curBoy = curBoy.getNext();
        }
    }

    /**
     * 根据用户的输入，计算出小孩出圈的顺序
     * @param startNo   开始数数的位置
     * @param countNum  数几下
     * @param nums      圈中最初小孩的数量
     */
    public void countBoy(int startNo,int countNum,int nums){
        if(first == null || startNo < 1 || startNo > nums){
            System.out.println("The params is Error , please repeat input");
            return;
        }
        //创建辅助指针，帮助完成小孩出圈，辅助指针指向最后一个节点
        Boy helper = first;
        while (true){
            if(helper.getNext() == first){
                break;
            }
            helper = helper.getNext();
        }
        //小孩报数前，先让first 和helper 移动 k-1 次
        for (int j = 0; j < startNo - 1; j++) {
            first = first.getNext();
            helper = helper.getNext();
        }
        //当小孩报数时，让first和helper 指针同时移动 m-1 次，然后出圈
        while (true){
            if(helper == first){
                break;
            }
            //让first和helper指针同时移动 countNum-1
            for (int i = 0; i < countNum - 1; i++) {
                first = first.getNext();
                helper = helper.getNext();
            }
            //这时first指向的节点，就是要出圈的小孩节点
            System.out.printf("The out  boy is %d\n",first.getNo());
            //重新设置小孩节点
            first = first.getNext();
            helper.setNext(first);
        }

        System.out.printf("The last boy is %d\n",first.getNo());
    }

}


class Boy{
    private int no;
    private Boy next;

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public Boy getNext() {
        return next;
    }

    public void setNext(Boy next) {
        this.next = next;
    }

    public Boy(int no) {
        this.no = no;
    }
}
